package rit.edu.cjp5465.simpGrocList;

import java.sql.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.format.Time;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class SimpleGroceryListActivity extends ListActivity {
	private static final int Create_List_ID = Menu.FIRST;
	private static final int Delete_ID = Menu.FIRST;
	private DBAdapter db;
	private Cursor listCursor;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		db = new DBAdapter(this);
		fillData();
		
		registerForContextMenu(getListView());	
	}

	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    menu.add(0, Delete_ID, 0, R.string.menu_delete);
	}
	
	public boolean onContextItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
	    case Delete_ID:
	        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        listCursor.moveToPosition(info.position);
	        db.open();
	        db.deleteList( listCursor.getLong(0));
	        db.close();
	        fillData();
	        return true;
	    }
	    return super.onContextItemSelected(item);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, Create_List_ID, 0, R.string.create_list);
        return result;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case Create_List_ID:
            createList();
            return true;
        }

        return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l, v, position, id);
		
		listCursor.moveToPosition(position);
		Intent i = new Intent(this, ShoppingList.class);
		i.putExtra(DBAdapter.CL_ROWID, listCursor.getLong( listCursor.getColumnIndexOrThrow(DBAdapter.CL_ROWID) ));
		i.putExtra(DBAdapter.CL_NAME, listCursor.getString( listCursor.getColumnIndexOrThrow(DBAdapter.CL_NAME)));
		i.putExtra(DBAdapter.CL_SL_DATE, listCursor.getString( listCursor.getColumnIndexOrThrow(DBAdapter.CL_SL_DATE) ));
		startActivity(i);
	}
	
	private void createList(){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("New List");
		alert.setMessage("Name of List");

		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		  Editable value = input.getText();
		  Time now = new Time();
			now.setToNow();
			db.open();
			db.insertList(value.toString(), now.toString());
			db.close();
			fillData();
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		    // Canceled.
		  }
		});

		alert.show();		
	}
	
	private void fillData(){
		 // Get all of the notes from the database and create the item list
		db.open();
        listCursor = db.getAllLists();
        startManagingCursor(listCursor);

        String[] from = new String[] { DBAdapter.CL_NAME };
        int[] to = new int[] { R.id.text1 };
        
        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.grocery_item_row, listCursor, from, to);
        setListAdapter(notes);
        db.close();
	}
}